package cn.kgc.itrip.service.impl;

import cn.kgc.itrip.common.RedisUtils;
import cn.kgc.itrip.dto.Dto;
import cn.kgc.itrip.dto.ItripUserVO;
import cn.kgc.itrip.mapper.userEmail.UserEmailMapper;
import cn.kgc.itrip.pojo.ItripUser;
import cn.kgc.itrip.common.DtoUtil;
import cn.kgc.itrip.common.ErrorCode;
import cn.kgc.itrip.common.MD5;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.regex.Pattern;

@Service
public class UserEmailServiceImpl  implements UserEmailService{

    @Resource
    private UserEmailMapper userEmailMapper;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private EmailService emailService;


    /**
     * 邮箱注册
     * @param itripUserVO
     * @return
     */
    @Override
    public Dto itripDoRegister(ItripUserVO itripUserVO)throws Exception{
            //判断是否为Email邮箱格式
            if (!validEamil(itripUserVO.getUserCode())){
                return DtoUtil.returnFail("邮箱验证不通过",ErrorCode.AUTH_ILLEGAL_USERCODE);
            }
            //判断，邮箱是否被注册
            if (null==userEmailMapper.itripDoRegister(itripUserVO.getUserCode())){
                //如果没有被注册，生成验证码
                String md5 = MD5.getMd5(new Date().toLocaleString(), 32);
                //将激活码存储到redis中
                redisUtils.setExpire(itripUserVO.getUserCode(),md5,60*10);
                //使用邮箱发送给客户
                emailService.sendToEmail(itripUserVO.getUserCode(),md5);
                //将注册的用户信息添加到数据库中
                ItripUser userVO=new ItripUser();
                userVO.setUserCode(itripUserVO.getUserCode());
                userVO.setUserName(itripUserVO.getUserName());
                userVO.setUserPassword(MD5.getMd5(itripUserVO.getUserPassword(),32));
                userVO.setCreationDate(new Date());
                userVO.setUserType(0);
                userVO.setActivated(0);
                userEmailMapper.userAdd(userVO);
                return DtoUtil.returnSuccess("邮箱注册成功");
            }
            return DtoUtil.returnFail("注册失败",ErrorCode.AUTH_USER_ALREADY_EXISTS);
    }


    /**
     * 正则判断是否为email
     * @param userCode
     * @return
     * @throws Exception
     */
    public boolean validEamil(String userCode) throws Exception{
        String reg = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        return Pattern.compile(reg).matcher(userCode).find();
    }


    /**
     * 激活邮箱
     * @param userCode
     * @param code
     * @return
     */
    @Override
    public Dto activateEmail(String userCode, String code) {
        //1.判断激活码是否存在
        if (redisUtils.isExists(userCode)){
            String redisValue = redisUtils.getValue(userCode);
            //2.判断激活码是否一致
            if (code.equals(redisValue)){
                //3.查找用户
                ItripUser user=userEmailMapper.findUser(userCode);
                //4.改变用户激活状态
                user.setActivated(1);
                user.setFlatID(user.getId());
                user.setModifyDate(new Date());
                userEmailMapper.userUpdate(user);
                //5.返回信息
                return DtoUtil.returnSuccess("激活成功");
            }
        }
        return DtoUtil.returnFail("激活失败",ErrorCode.AUTH_AUTHENTICATION_FAILED);
    }
}
